package com.xysq.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CustomerCouponBO;
import com.umeng.analytics.MobclickAgent;
import com.xysq.activity.TipsActivity;
import com.xysq.adapter.MyPublishedAdapter;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;
import com.xysq.widget.LoadData;
import com.xysq.widget.ScrollListener;

import java.util.List;

public class MyPublishedFragment extends BaseFragment {

    public static final String TYPE_SALING = "C01";
    public static final String TYPE_SOLD_OUT = "C02";
    private static final String PARAM_TYPE = "type";
    private String type;
    private TextView emptyTxt;
    private ProgressBar progressBar;
    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView publishedList;
    private MyPublishedAdapter publishedAdapter;
    ScrollListener scrollListener;

    public static MyPublishedFragment newInstance(String type) {
        MyPublishedFragment fragment = new MyPublishedFragment();
        Bundle args = new Bundle();
        args.putString(PARAM_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    public MyPublishedFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString(PARAM_TYPE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_order_list, container, false);
        emptyTxt = (TextView) rootView.findViewById(R.id.txt_empty);
        progressBar = (ProgressBar) rootView.findViewById(R.id.progress_loading);
        swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe);
        publishedList = (ListView) rootView.findViewById(R.id.list_order);
        publishedAdapter = new MyPublishedAdapter(this, getActivity(), appAction, type);
        publishedList.setAdapter(publishedAdapter);
        publishedList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CustomerCouponBO couponBO = (CustomerCouponBO) publishedAdapter.getItem(position);
                Intent intent = new Intent(getActivity(), TipsActivity.class);
                intent.putExtra(TipsActivity.COUPONMODEL_ID, couponBO.getId());
                intent.putExtra(TipsActivity.CUSTOMER_ID, couponBO.getCustomerId());
                startActivity(intent);
            }
        });
        scrollListener = new ScrollListener(publishedList, true, new LoadData() {
            @Override
            public void loadMyData() {
                loadData();
            }
        });
        publishedList.setOnScrollListener(scrollListener);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                reloadData();
            }
        });

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void reloadData() {
        scrollListener.setCurrentPage(1);
        publishedAdapter.clearItems();
        loadData();
    }

    public void loadData() {
        progressBar.setVisibility(View.VISIBLE);
        String customerId = UserInfoKeeper.readCustomerId(getActivity());
        appAction.listMyPublishedCoupons(customerId, type, scrollListener.getCurrentPage(), new CallbackListener<List<CustomerCouponBO>>() {
            @Override
            public void onSuccess(List<CustomerCouponBO> data) {
                if (scrollListener.getCurrentPage() == CURRENT_PAGE) {
                    if (data.size() == 0) {
                        emptyTxt.setVisibility(View.VISIBLE);
                    } else {
                        emptyTxt.setVisibility(View.GONE);
                        publishedAdapter.setItems(data);
                    }
                } else {
                    emptyTxt.setVisibility(View.GONE);
                    if (data.size() > 0) {
                        publishedAdapter.addItems(data);
                    }
                }
                scrollListener.setLoadFinish(true);
                swipeRefreshLayout.setRefreshing(false);
                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(getActivity(), message);
                swipeRefreshLayout.setRefreshing(false);
                progressBar.setVisibility(View.GONE);
                scrollListener.setLoadFinish(true);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        reloadData();
        if (type == TYPE_SALING) {
            MobclickAgent.onPageStart("我的宝贝出售中");
        }
        if (type == TYPE_SOLD_OUT) {
            MobclickAgent.onPageStart("我的宝贝已售完");
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }
}
